//
//  PageContentView.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/18.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit
import RxSwift


class PageContentView: UIView {
    
    private let childs: [UIViewController]
    private weak var parent: UIViewController?
    
    private lazy var scrollView: UIScrollView = {
        let v = UIScrollView()
        v.bounces = false
        v.isPagingEnabled = true
        v.scrollsToTop = false
        v.showsVerticalScrollIndicator = false
        v.showsHorizontalScrollIndicator = false
        v.delegate = self
        return v
    }()
    
    var pageIndex: Int = 0
    
    let scrollIndexSubject = PublishSubject<Int>()
    let willDisplaySubject = PublishSubject<(left: Int, right: Int)>()
    
    init(childs: [UIViewController], parent: UIViewController) {
        self.childs = childs
        self.parent = parent
        super.init(frame: .zero)
        backgroundColor = .commonBackgroundColor
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let views = childs.compactMap { $0.view }
        var previous: UIView?
        for v in views {
            v.frame = scrollView.bounds
            v.x += previous?.maxX ?? 0
            previous = v
        }
        scrollView.contentSize = CGSize(width: width * CGFloat(childs.count), height: height)
    }
    
}

extension PageContentView {
    func autoScrollTo(_ index: Int) {
        if index == pageIndex { return }
        pageIndex = index
        let offsetX = CGFloat(index) * scrollView.width
        scrollView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: false)
    }
}


private extension PageContentView {
    func setupSubviews() {
        addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        
        if let parent = parent {
            childs.forEach {
                parent.addChild($0)
                scrollView.addSubview($0.view)
                $0.didMove(toParent: parent)
            }
        }
    }
    
    func manualScrollTo(_ index: Int) {
        if pageIndex == index { return }
        pageIndex = index
        scrollIndexSubject.onNext(pageIndex)
    }
    
    func manualScrollWillDisplay(_ leftIndex: Int, _ rightIndex: Int) {
        willDisplaySubject.onNext((leftIndex, rightIndex))
    }
}


extension PageContentView: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        let pageIndex = Int((offsetX + scrollView.centerX) / scrollView.width)
        manualScrollTo(pageIndex)
        let remainder = offsetX.truncatingRemainder(dividingBy: scrollView.width)
        if remainder > 0 {
            let left = Int(offsetX / scrollView.width)
            let right = left + 1
            manualScrollWillDisplay(left, right)
        }
    }
}

